package com.ruoyi.project.manage.approveLog.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.manage.file.domain.ProjectFile;
import com.ruoyi.project.manage.file.mapper.ProjectFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.manage.approveLog.mapper.ApproveLogMapper;
import com.ruoyi.project.manage.approveLog.domain.ApproveLog;
import com.ruoyi.project.manage.approveLog.service.IApproveLogService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 审批流转记录Service业务层处理
 * 
 * @author fangkun
 * @date 2022-03-28
 */
@Service
@Transactional
public class ApproveLogServiceImpl implements IApproveLogService 
{
    static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private ApproveLogMapper approveLogMapper;
    @Autowired
    private ProjectFileMapper projectFileMapper;

    /**
     * 查询审批流转记录
     * 
     * @param approveLogId 审批流转记录主键
     * @return 审批流转记录
     */
    @Override
    public ApproveLog selectApproveLogByApproveLogId(Long approveLogId)
    {
        return approveLogMapper.selectApproveLogByApproveLogId(approveLogId);
    }

    /**
     * 查询审批流转记录列表
     * 
     * @param approveLog 审批流转记录
     * @return 审批流转记录
     */
    @Override
    public List<ApproveLog> selectApproveLogList(ApproveLog approveLog)
    {
        return approveLogMapper.selectApproveLogList(approveLog);
    }

    @Override
    public List<ApproveLog> selectProjectLogList(Long projectId, String period) {
        String beginDate = "";
        String endDate = "";

        Calendar cal = Calendar.getInstance();

        if (StringUtils.equalsIgnoreCase(ProjectConstants.PERIOD_WEED, period)) {
            cal.setFirstDayOfWeek(Calendar.MONDAY);
            cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            beginDate = df.format(cal.getTime());
            cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            endDate = df.format(cal.getTime());
        } else if (StringUtils.equalsIgnoreCase(ProjectConstants.PERIOD_MONTH, period)) {
            endDate = df.format(cal.getTime());
            cal.set(Calendar.DAY_OF_MONTH, 1);
            beginDate = df.format(cal.getTime());
        } else if (StringUtils.equalsIgnoreCase(ProjectConstants.PERIOD_LAST_MONTH, period)) {
            cal.add(Calendar.MONTH, -1);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            beginDate = df.format(cal.getTime());

            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            endDate = df.format(cal.getTime());
        } else if (StringUtils.equalsIgnoreCase(ProjectConstants.PERIOD_SEASON, period)) {
            endDate = df.format(cal.getTime());

            // 获取季度信息
            cal.set(Calendar.MONTH, (cal.get(Calendar.MONTH) /3) * 3);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            beginDate = df.format(cal.getTime());
        } else if (StringUtils.equalsIgnoreCase(ProjectConstants.PERIOD_YEAR, period)) {
            endDate = df.format(cal.getTime());

            cal.set(Calendar.DAY_OF_YEAR, 1);
            beginDate = df.format(cal.getTime());
        } else {
            // 全部，什么都不做
        }
        if (!StringUtils.isBlank(endDate)) {
            endDate = endDate + " 23:59:59";
        }
        return approveLogMapper.selectProjectLogList(projectId, beginDate, endDate);
    }

    /**
     * 新增审批流转记录
     * 
     * @param approveLog 审批流转记录
     * @return 结果
     */
    @Override
    public int insertApproveLog(ApproveLog approveLog)
    {

        approveLog.setEntityType(ProjectConstants.APPROVAL_TYPE_LOG);
        approveLog.setCreateTime(DateUtils.getNowDate());
        approveLog.setCreateBy("审核员:" + ShiroUtils.getSysUser().getUserName());
        approveLog.setUpdateTime(DateUtils.getNowDate());
        approveLog.setUpdateBy(ShiroUtils.getLoginName());
        int i = approveLogMapper.insertApproveLog(approveLog);

        // 处理文件数据
        List<ProjectFile> fileList = approveLog.getFileList();
        if (fileList != null && fileList.size() > 0) {
            for (ProjectFile projectFile : fileList) {
                projectFile.setProjectId(approveLog.getEntityId());
                projectFile.setProjectFileType((int) ProjectConstants.APPROVAL_TYPE_LOG);
                projectFile.setProjectItemId(null);
                projectFile.setApproveLogId(approveLog.getApproveLogId());
                projectFile.setStatus(UserConstants.NORMAL);
                projectFile.setCreateTime(DateUtils.getNowDate());
                projectFile.setCreateBy(ShiroUtils.getLoginName());
                projectFile.setUpdateTime(DateUtils.getNowDate());
                projectFile.setUpdateBy(ShiroUtils.getLoginName());

                projectFileMapper.insertProjectFile(projectFile);
            }
        }

        return i;
    }

    /**
     * 修改审批流转记录
     * 
     * @param approveLog 审批流转记录
     * @return 结果
     */
    @Override
    public int updateApproveLog(ApproveLog approveLog)
    {
        approveLog.setUpdateTime(DateUtils.getNowDate());
        return approveLogMapper.updateApproveLog(approveLog);
    }

    /**
     * 批量删除审批流转记录
     * 
     * @param approveLogIds 需要删除的审批流转记录主键
     * @return 结果
     */
    @Override
    public int deleteApproveLogByApproveLogIds(String approveLogIds)
    {
        return approveLogMapper.deleteApproveLogByApproveLogIds(Convert.toStrArray(approveLogIds));
    }

    /**
     * 删除审批流转记录信息
     * 
     * @param approveLogId 审批流转记录主键
     * @return 结果
     */
    @Override
    public int deleteApproveLogByApproveLogId(Long approveLogId)
    {
        return approveLogMapper.deleteApproveLogByApproveLogId(approveLogId);
    }
}
